from django.shortcuts import render
from rest_framework.generics import ListAPIView
from rest_framework.views import APIView
from rest_framework.response import Response
from crouse.models import CategoryModel,CrouseDeskModel
from crouse.crouseser import CategorySerializer,Category,CrouseSerializer,CourseIndexSerializer
from rest_framework.pagination import PageNumberPagination
from django.db.models import Q

# Create your views here.

class CategroyView(APIView):

    def get(self,request):
        allcate = CategoryModel.objects.filter(parent=None,isshow=True,ishome=True).order_by("order")
        ser = CategorySerializer(allcate,many=True)
        for i in ser.data:
            i["childern"] = i["childern"][:3]
        return Response({"massage":"success","data":ser.data,"code":200})

class NowCate(APIView):
    def get(self,request):
        id = request.query_params.get('id')
        info = CategoryModel.objects.filter(id=id,parent=None,isshow=True,ishome=True).first()
        ser = CategorySerializer(info)
        twocate = CategoryModel.objects.filter(parent=id,isshow=True).values_list('id',flat=True)
        course = CrouseDeskModel.objects.filter(cates__in=twocate,isshow=True,ishome=True).order_by('order').values()[:4]
        cuorseinfo = ser.data
        cuorseinfo['course'] = course
        return Response({"massage":"success","data":cuorseinfo,"code":200})

class Direction(APIView):
    def get(self,request):
        allcate = CategoryModel.objects.filter(parent=None,isshow=True).order_by('order')
        ser = CategorySerializer(allcate,many=True)
        return Response({"massage":"success","data":ser.data,"code":200})
class ChidCate(APIView):
    def get(self,request):
        pid = int(request.query_params.get('pid'))
        if pid== 0:
            child = CategoryModel.objects.exclude(parent=None,isshow=False).order_by('order')
        else:
            child =CategoryModel.objects.filter(parent=pid,isshow=True).order_by('order')
        ser = CategorySerializer(child,many=True)
        return Response({"massage":"ok","data":ser.data,"code":200})

class Pagination(PageNumberPagination):
    page_size = 3
    page_size_query_param = "size"
    page_query_param = "page"


class CrouseListView(ListAPIView):
    queryset = CrouseDeskModel.objects.all()
    serializer_class = CrouseSerializer

    pagination_class = Pagination
    def get_queryset(self):
        direction = int(self.request.query_params.get("direction"))
        cate = int(self.request.query_params.get("cate"))
        level = int(self.request.query_params.get("level"))
        order = self.request.query_params.get("ordering")
        print(order)
        query = Q()
        if cate != 0:
            query&= Q(cates=cate)
        if direction>0 and cate == 0:
             allcate = CategoryModel.objects.filter(parent_id=direction).values()
             # child = []
             # for i in allcate:
             #    child.append(i['id'])
             query&=Q(cates__in=allcate)
        if level > 0:
            query&= Q(level=level)
        if order:
            return self.queryset.filter(query).order_by(order)
        else:
            return self.queryset.filter(query)

class Detail(APIView):
    def get(self,request):
        id = request.query_params.get('id')
        print(id)
        info = CrouseDeskModel.objects.filter(id=id).first()
        info.read += 1
        ser = CrouseSerializer(info)
        return Response({"massage":"success","data":ser.data,"code":200})

from drf_haystack.viewsets import HaystackViewSet
from utils.myredis import redistoll
class HotWordView(HaystackViewSet):
    index_models = [CrouseDeskModel]
    serializer_class = CourseIndexSerializer
    pagination_class = Pagination

    def list(self,request,*args,**kwargs):
        text = request.query_params.get('text','')
        if text:
            redistoll.hotadd('09hot',text)
        data = super().list(request,*args,**kwargs).data
        for i in data['results']:
            i['cimg'] = 'http://127.0.0.1:8000/uploads'+i['cimg']
        return Response(data)

class HotWordGet(APIView):
    def get(self,request):
        data = redistoll.hotget('09hot')[:4]
        return Response(data)




